package io.github.hadyang.leetcode;

import java.util.Objects;

public class TreeNode {
  public int val;
  public TreeNode left;
  public TreeNode right;

  public TreeNode(int x) {
    val = x;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    TreeNode node = (TreeNode) o;
    return val == node.val && Objects.equals(left, node.left) && Objects.equals(right, node.right);
  }

  @Override
  public int hashCode() {
    return Objects.hash(val, left, right);
  }

  @Override
  public String toString() {
    return toString(this);
  }

  private String toString(TreeNode node) {
    String out = "";
    if (node != null) {
      out += node.val + " ";
      out += toString(node.left);
      out += toString(node.right);
    }
    return out;
  }
}
